﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void PageLoad()
        {
            DataTable dt = new DataTable();
            
            PhongBanBUS phongban_bus = new PhongBanBUS();            
            dt = phongban_bus.sp_PhongBan();            
            
            cbPhongBan.DisplayMember="TenPhongBan";
            cbPhongBan.ValueMember = "MaPhongBan";
            cbPhongBan.DataSource = dt;
            

            ViTriBUS vitri_bus = new ViTriBUS();
            dt = vitri_bus.sp_ViTri();            
            cbViTri.ValueMember = "MaViTri";
            cbViTri.DisplayMember = "TenViTri";
            cbViTri.DataSource = dt;

            TrangThaiBUS trangthai_bus = new TrangThaiBUS();
            dt = trangthai_bus.sp_TrangThai();
            cbTrangThai.ValueMember = "MaTrangThai";
            cbTrangThai.DisplayMember = "TenTrangThai";
            cbTrangThai.DataSource = dt;

            cbphai.Items.Add("Nữ");
            cbphai.Items.Add("Nam");

            cbphaitn.Items.Add("Nữ");
            cbphaitn.Items.Add("Nam");

            LoadGridNV();
            LoadGridBH();
            LoadcboLoaiBaoHiem();
            //LoadGridTN();
           
        }
    
        public DataTable LoadGridNV()
        {
            DataTable dt = new DataTable();
            NhanVienBUS nhanvien_bus = new NhanVienBUS();
            dt = nhanvien_bus.sp_DanhSachNhanVien();
            GridDSNV.DataSource = dt;
            GridNVNPBH.DataSource = dt;
            return dt;
        }
                
        public void LoadGridBH()
        {
            DataTable dt = new DataTable();
            LoaiBaoHiemBUS baohiem_bus = new LoaiBaoHiemBUS();
            dt = baohiem_bus.sp_LayLoaiBaoHiem();            
            GridBH.DataSource = dt;           
        }


        public void LoadcboLoaiBaoHiem()
        {
            DataTable dt = new DataTable();
            LoaiBaoHiemBUS baohiem_bus = new LoaiBaoHiemBUS();
            dt = baohiem_bus.sp_LayLoaiBaoHiem();
            cboLoaiBaoHiem.DataSource = dt;
            cboLoaiBaoHiem.DisplayMember = "TenLoaiBaoHiem";
            cboLoaiBaoHiem.ValueMember = "MaLoaiBaoHiem";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PageLoad();
        }
        
        private void GridDSNV_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtmanv.Text = GridDSNV.CurrentRow.Cells[0].Value.ToString();
            txtTen.Text = GridDSNV.CurrentRow.Cells[1].Value.ToString();
            txtdiachi.Text = GridDSNV.CurrentRow.Cells[2].Value.ToString();
            txtdienthoai.Text = GridDSNV.CurrentRow.Cells[3].Value.ToString();
            try
            {
                cbngay.Value = DateTime.Parse(GridDSNV.CurrentRow.Cells[4].Value.ToString());
            }
            catch { }
            txtemail.Text = GridDSNV.CurrentRow.Cells[5].Value.ToString();
            txtpass.Text = GridDSNV.CurrentRow.Cells[7].Value.ToString();
            cbphai.Text = GridDSNV.CurrentRow.Cells[6].Value.ToString();
            cbViTri.Text = GridDSNV.CurrentRow.Cells[8].Value.ToString();
            cbTrangThai.Text = GridDSNV.CurrentRow.Cells[9].Value.ToString();
            cbPhongBan.Text = GridDSNV.CurrentRow.Cells[10].Value.ToString();
            txttaikhoan.Text = GridDSNV.CurrentRow.Cells[11].Value.ToString();
            
            LoadGridTN(GridDSNV.CurrentRow.Cells[0].Value.ToString());
            LoadGridDanhSachPhuCap(GridDSNV.CurrentRow.Cells[0].Value.ToString());
        }

        private void btnThemNV_Click(object sender, EventArgs e)
        {
            try
            {
                NhanVienDTO nhavien_dto = new NhanVienDTO();
                nhavien_dto.TenNhanVien = txtTen.Text;
                nhavien_dto.TaiKhoanNganHang = txttaikhoan.Text;
                nhavien_dto.Phai = cbphai.SelectedItem.ToString();
                nhavien_dto.PassWord = txtpass.Text;
                nhavien_dto.DiaChi = txtdiachi.Text;
                nhavien_dto.DienThoai = txtdienthoai.Text;
                nhavien_dto.Email = txtemail.Text;
                nhavien_dto.NgaySinh = cbngay.Value;
                nhavien_dto.MaVitri = int.Parse(cbViTri.SelectedValue.ToString());
                nhavien_dto.MaTrangThai = int.Parse(cbTrangThai.SelectedValue.ToString());
                nhavien_dto.MaPhongBan = int.Parse(cbPhongBan.SelectedValue.ToString());

                NhanVienBUS nhanvien_bus = new NhanVienBUS();
                nhanvien_bus.sp_ThemNhanVien(nhavien_dto);

                LoadGridNV();
                txtTen.Text = "";
                txtdiachi.Text = "";
                txtdienthoai.Text = "";
                txtemail.Text = "";
                txtpass.Text = "";
                txtTenPhuCap.Text = "";
                txttaikhoan.Text= "";
                
            }
            catch (Exception   ex)
            { MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); }
        }
        private void btnSuaNV_Click(object sender, EventArgs e)
        {
            try
            {
                NhanVienDTO nhavien_dto = new NhanVienDTO();
                nhavien_dto.MaNhanVien = GridDSNV.CurrentRow.Cells[0].Value.ToString();            
                nhavien_dto.TenNhanVien = txtTen.Text;
                nhavien_dto.TaiKhoanNganHang = txttaikhoan.Text;
                nhavien_dto.Phai = cbphai.Text;
                nhavien_dto.PassWord = txtpass.Text;
                nhavien_dto.DiaChi = txtdiachi.Text;
                nhavien_dto.DienThoai = txtdienthoai.Text;
                nhavien_dto.Email = txtemail.Text;
                nhavien_dto.NgaySinh = cbngay.Value;
                nhavien_dto.MaVitri = int.Parse(cbViTri.SelectedValue.ToString());
                nhavien_dto.MaTrangThai = int.Parse(cbTrangThai.SelectedValue.ToString());
                nhavien_dto.MaPhongBan = int.Parse(cbPhongBan.SelectedValue.ToString());

                NhanVienBUS nhanvien_bus = new NhanVienBUS();
                nhanvien_bus.sp_CapNhatNhanVien(nhavien_dto);

                LoadGridNV();
                txtTen.Text = "";
                txtdiachi.Text = "";
                txtdienthoai.Text = "";
                txtemail.Text = "";
                txtpass.Text = "";
                txtTenPhuCap.Text = "";
                txttaikhoan.Text = "";
            }
            catch (Exception ex)
                { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }    
        }

        public DataTable LoadGridTN(string MaNhanVien)
        {
            DataTable dt = new DataTable();
            ThanNhanBUS thannhan_bus = new ThanNhanBUS();
            NhanVienDTO nhanvien = new NhanVienDTO();
            nhanvien.MaNhanVien = MaNhanVien;
            dt = thannhan_bus.sp_DanhSachThanNhan(nhanvien);
            GridTN.DataSource = dt;
            return dt;
        }

        
        private void GridTN_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txttentn.Text = GridTN.CurrentRow.Cells["TenThanNhan"].Value.ToString();
            try
            {
                cbngaysinh.Value = DateTime.Parse(GridTN.CurrentRow.Cells["NgaySinh"].Value.ToString());
            }
            catch { }
            cbphaitn.Text = GridTN.CurrentRow.Cells["Phai"].Value.ToString();
            txtmanv.Text = GridDSNV.CurrentRow.Cells[0].Value.ToString();
            
        }
        private void btnThemtn_Click(object sender, EventArgs e)
        {
            try
            {

                ThanNhanDTO thannhan_dto = new ThanNhanDTO();
                thannhan_dto.TenThanNhan = txttentn.Text;
                thannhan_dto.Phai = cbphaitn.Text.ToString();
                thannhan_dto.NgaySinh = cbngaysinh.Value;
                thannhan_dto.MaNhanVien = txtmanv.Text;

                ThanNhanBUS thannhan_bus = new ThanNhanBUS();
                thannhan_bus.sp_ThemThanNhan(thannhan_dto);


                LoadGridTN(txtmanv.Text);
                txttentn.Text = "";
                //txtmanv.Text = "";
                
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
        private void btnsuatn_Click(object sender, EventArgs e)
        {
            try
            {
                ThanNhanDTO thannhan_dto = new ThanNhanDTO();
                thannhan_dto.MaThanNhan = int.Parse(GridTN.CurrentRow.Cells[0].Value.ToString());
                thannhan_dto.TenThanNhan = txttentn.Text;
                thannhan_dto.Phai = cbphaitn.Text.ToString();
                thannhan_dto.NgaySinh = DateTime.Parse( cbngaysinh.Value.ToString());
                thannhan_dto.MaNhanVien = txtmanv.Text;

                ThanNhanBUS thannhan_bus = new ThanNhanBUS();
                thannhan_bus.sp_CapNhatThanNhan(thannhan_dto);


                LoadGridTN(txtmanv.Text);
                txttentn.Text = "";
                //txtmanv.Text = "";
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
        private void btnxoatn_Click(object sender, EventArgs e)
        {
            ThanNhanDTO thannhan = new ThanNhanDTO();
            thannhan.MaThanNhan = int.Parse(GridTN.CurrentRow.Cells[1].Value.ToString());

            ThanNhanBUS thannhan_bus = new ThanNhanBUS();
            thannhan_bus.sp_XoaThanNhan(thannhan);

            LoadGridTN(txtmanv.Text);
        }

        public void LoadGridDanhSachPhuCap(string MaNhanVien)
        {
            PhuCapBUS phucap_Bus = new PhuCapBUS();
            DataTable dt = new DataTable();
            dt = phucap_Bus.sp_LayPhuCapMotNhanVien(MaNhanVien);
            gridDanhSachPhuCap.DataSource = dt;
            gridDanhSachPhuCap.AutoGenerateColumns = false;
        }
        private void btnThemPhuCap_Click(object sender, EventArgs e)
        {
            PhuCapDTO PhuCap = new PhuCapDTO();
            PhuCap.MaNhanVien = GridDSNV.CurrentRow.Cells[0].Value.ToString();
            PhuCap.TenPhuCap = txtTenPhuCap.Text;
            try
            {
                PhuCap.SoTienPhuCap = double.Parse(txtTienPhuCap.Text);
            }
            catch
            {
                MessageBox.Show("Chưa nhập số tiền phụ cấp!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            PhuCap.NgayPhuCap = DateTime.Parse(dtNgayPhuCap.Value.ToShortDateString());

            PhuCapBUS phucap_Bus = new PhuCapBUS();
            try
            {
                phucap_Bus.sp_ThemPhuCap(PhuCap);
                LoadGridDanhSachPhuCap(PhuCap.MaNhanVien);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            txtTenPhuCap.Text = "";
            txtTienPhuCap.Text = "";

        }

        private void btnXoaPhuCap_Click(object sender, EventArgs e)
        {
            int MaPhuCap = int.Parse(gridDanhSachPhuCap.CurrentRow.Cells[3].Value.ToString());
            PhuCapBUS phucap_bus = new PhuCapBUS();
            phucap_bus.sp_XoaPhuCap(MaPhuCap);
            LoadGridDanhSachPhuCap(GridDSNV.CurrentRow.Cells[0].Value.ToString());

            txtTenPhuCap.Text = "";
            txtTienPhuCap.Text = "";
            
        }

        private void btnCapNhatPhuCap_Click(object sender, EventArgs e)
        {
            PhuCapDTO PhuCap = new PhuCapDTO();
            PhuCap.MaPhuCap = int.Parse(gridDanhSachPhuCap.CurrentRow.Cells[3].Value.ToString());
            PhuCap.MaNhanVien = GridDSNV.CurrentRow.Cells[0].Value.ToString();
            PhuCap.TenPhuCap = txtTenPhuCap.Text;
            PhuCap.SoTienPhuCap = double.Parse(txtTienPhuCap.Text);
            PhuCap.NgayPhuCap = DateTime.Parse(dtNgayPhuCap.Value.ToShortDateString());

            PhuCapBUS phucap_Bus = new PhuCapBUS();
            phucap_Bus.sp_CapNhatPhuCap(PhuCap);
            LoadGridDanhSachPhuCap(PhuCap.MaNhanVien);

            txtTenPhuCap.Text = "";
            txtTienPhuCap.Text = "";
        }

        private void dataGridView5_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                txtTenPhuCap.Text = gridDanhSachPhuCap.CurrentRow.Cells[0].Value.ToString();
                txtTienPhuCap.Text = gridDanhSachPhuCap.CurrentRow.Cells[2].Value.ToString();
                dtNgayPhuCap.Value = DateTime.Parse(gridDanhSachPhuCap.CurrentRow.Cells[1].Value.ToString());
            }
            catch { }
        }

        private void GridNVNPBH_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                txtTenNhanVien.Text = GridNVNPBH.CurrentRow.Cells[0].Value.ToString();
                
                //txtTenNhanVien.Text = GridNVNPBH.CurrentRow.Cells["TenNhanVien"].Value.ToString();
                txtNgaySinh.Text = GridNVNPBH.CurrentRow.Cells[2].Value.ToString();
            }
            catch
            {
            
            }
        }

        private void btThemBH_Click(object sender, EventArgs e)
        {
            LoaiBaoHiemDTO baohiem = new LoaiBaoHiemDTO();
            baohiem.TenLoaiBaoHiem = txtTenLoaiBaoHiem.Text;

            LoaiBaoHiemBUS baohiem_bus = new LoaiBaoHiemBUS();
            try
            {
                baohiem_bus.sp_ThemLoaiBaoHiem(baohiem);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            txtTenLoaiBaoHiem.Text = "";
            LoadGridBH();
        }

        private void btCapNhatBH_Click(object sender, EventArgs e)
        {
            if (txtTenLoaiBaoHiem.Text == "")
                MessageBox.Show("Chưa nhập Tên Loại Bảo Hiểm!");
            else
            {
                LoaiBaoHiemDTO baohiem_dto = new LoaiBaoHiemDTO();
                LoaiBaoHiemBUS baohiem_bus = new LoaiBaoHiemBUS();
                baohiem_dto.MaLoaiBaoHiem = int.Parse(GridBH.CurrentRow.Cells["MaLoaiBaoHiem"].Value.ToString());
                baohiem_dto.TenLoaiBaoHiem = txtTenLoaiBaoHiem.Text;
                baohiem_bus.sp_CapNhatLoaiBaoHiem(baohiem_dto);
                LoadGridBH();
                txtTenLoaiBaoHiem.Text = "";
            }
        }

        private void btXoaBH_Click(object sender, EventArgs e)
        {
            int MaLoaiBaoHiem = int.Parse(GridBH.CurrentRow.Cells["MaLoaiBaoHiem"].Value.ToString());
            LoaiBaoHiemBUS baohiem_bus = new LoaiBaoHiemBUS();
            LoaiBaoHiemDTO baohiem_dto=new LoaiBaoHiemDTO();
            baohiem_dto.MaLoaiBaoHiem=MaLoaiBaoHiem;
            baohiem_bus.sp_XoaLoaiBaoHiem(baohiem_dto);
            LoadGridBH();
            txtTenLoaiBaoHiem.Text = "";          
        }

        private void btGanBH_Click(object sender, EventArgs e)
        {
            //ChiTietBaoHiemDTO chitietbaohiem_dto = new ChiTietBaoHiemDTO();
            //ChiTietBaoHiemBUS chitetbaohiem_bus = new ChiTietBaoHiemBUS();
            //try
            //{
            //    string MaNhanVien = GridNVNPBH.CurrentRow.Cells[0].Value.ToString();
            //    int MaLoaiBaoHiem = int.Parse(cboLoaiBaoHiem.SelectedValue.ToString());
                
            //    DateTime NgayThamGia;
            //    DateTime.TryParse(dateTimePicker1.Text.ToString(), out NgayThamGia );
            //    int TiLeDong;
            //    int.TryParse(txtTiLeDong.Text, out TiLeDong);

            //    chitietbaohiem_dto.MaNhanVien = MaNhanVien;
            //    chitietbaohiem_dto.MaLoaiBaoHiem = MaLoaiBaoHiem;
            //    chitietbaohiem_dto.NgayThamGia = NgayThamGia;
            //    chitietbaohiem_dto.TiLeDong = TiLeDong;

            //    chitetbaohiem_bus.sp_ThemChiTietLoaiBaoHiem(chitietbaohiem_dto);
            //    MessageBox.Show("Gán loại bảo hiểm thành công!");
            //    LoadGridNV();
            //}
            //catch(Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}

        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            //ChiTietBaoHiemDTO chitietbaohiem_dto = new ChiTietBaoHiemDTO();
            //ChiTietBaoHiemBUS chitetbaohiem_bus = new ChiTietBaoHiemBUS();
            //try
            //{
            //    string MaNhanVien = GridNVNPBH.CurrentRow.Cells[0].Value.ToString();
            //    int MaLoaiBaoHiem = int.Parse(cboLoaiBaoHiem.SelectedItem.ToString());
            //    chitietbaohiem_dto.MaNhanVien = MaNhanVien;
            //    chitietbaohiem_dto.MaLoaiBaoHiem = MaLoaiBaoHiem;

            //    chitetbaohiem_bus.sp_XoaChiTietLoaiBaoHiem(chitietbaohiem_dto);
            //    MessageBox.Show("Xóa loại bảo hiểm thành công!");
            //    LoadGridNV();
            //}
            //catch(Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }

        private void GridBH_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                txtTenLoaiBaoHiem.Text = GridBH.CurrentRow.Cells["TenLoaiBaoHiem"].Value.ToString();
            }
            catch
            { 
            
            }
        }

        private void button22_Click(object sender, EventArgs e)
        {

        }

        private void toolStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            //if(toolStrip2.Items)
        }

        private void tsQuanLyNhanVien_Click(object sender, EventArgs e)
        {
            tbNV.SelectedTab = tbNhanVien;
        }

        private void tsQuanLyBaoHiem_Click(object sender, EventArgs e)
        {
            tbNV.SelectedTab = tbBaoHiem;
        }

        private void tsQuanLyPhep_Click(object sender, EventArgs e)
        {
            tbNV.SelectedTab = tbBaoHiem;
        }

        private void tsQuanLyLuong_Click(object sender, EventArgs e)
        {
            tbNV.SelectedTab = tbLuongThang;
        }

        private void tsPhongBan_Click(object sender, EventArgs e)
        {
            tbNV.SelectedTab = tbPhongBan;
        }
       
    }
}